from manim import *
import math
class work2(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(6*2+1):       
            dot1=Dot([-50,5*(i-6),0]).set_opacity(0.5)
            dot2=Dot([50,5*(i-6),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(10*2+1):        
            dot3=Dot([(i-10)*5,-30,0]).set_opacity(0.5)
            dot4=Dot([(i-10)*5,30,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        self.camera.frame.save_state()



         #画坐标轴

        axes = Axes(
            x_range=[-PI, PI, PI],  # 设置 x 轴范围，从 -3π 到 3π，步长为 π/2
            y_range=[-1.5, 1.5, 0.5],     # 设置 y 轴范围，从 -1.5 到 1.5，步长为 0.5
            axis_config={"color": WHITE,"stroke_width": 30},  # 设置坐标轴的颜色
            x_length=40,  # 控制 x 轴长度
            y_length=20 ,   # 控制 y 轴长度
        )
        axes.move_to([-25,-10,0])
        # 定义要绘制的函数
        # 定义要绘制的函数
        def my_function(x):
            return -math.exp(-x)+2
        graph1 = axes.plot(my_function, x_range=[-1, 0], color=BLUE, stroke_width=20)
        def my_function(x):
            return math.exp(x)
        graph2 = axes.plot(my_function, x_range=[0, 1], color=BLUE, stroke_width=20)
        # 定义直线函数
        def constant_line(x):
            return 0  # 这里的常数是示例值，你可以根据实际需要调整

        # 创建直线图像
        line_graph = axes.plot(constant_line, x_range=[-PI, 4*PI], color=PURE_RED, stroke_width=20)
        # 添加标题
        title = Text("f(x)",color=GREEN, font_size=240).move_to([-25,10,0])
        self.add(title)

        # 显示图像
        self.play(Create(axes),  Write(title),Create(graph1),Create(graph2))
        self.wait()


        text = Tex(r"\text{导数}",color=YELLOW_A
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([10,20,0])
        self.play(Write(text))
        arrow = Arrow(start=[10,18,0],end=[10,12,0],color=RED
                       , stroke_width=20, tip_length=1)

        self.play(GrowArrow(arrow))
        text1 = Tex(r"\text{变化率}",color=YELLOW_A
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([10,10,0])
        self.play(Write(text1))

        text2 = Tex(r"\text{某点是否可导}",color=YELLOW_B
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([10,20,0])
        self.play(
            ReplacementTransform(text,text2)
        )
        self.wait(0.5)
        text3 = Tex(r"\text{某点左右函数值变化率是否相等}",color=YELLOW_B
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([10,10,0])
        self.play(
            ReplacementTransform(text1,text3)
        )
        self.wait(0.5)
        text4 = Tex(r"\text{变限积分某点是否可导}",color=YELLOW_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([10,20,0])
        self.play(
            ReplacementTransform(text2,text4)
        )
        self.wait(0.5)
        text5 = Tex(r"\text{面积变化率是否相等}",color=YELLOW_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([10,10,0])
        self.play(
            ReplacementTransform(text3,text5)
        )
        self.wait(0.5)




        dot4=Dot([-30,5,0])
        dot5=Dot([-30,-15,0])
        line2=Line(dot4,dot5).set_color(YELLOW)
        line2.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line2))


# 计算阴影区域
        area2 = axes.get_area(graph1, [-0.76, 0], bounded_graph=line_graph, color=RED, opacity=0.5)
        self.play(line2.animate().scale(1).shift(RIGHT*4.8),Create(area2))
        area3 = axes.get_area(graph2, [0, 0.76], bounded_graph=line_graph, color=GREEN, opacity=0.5)
        self.play(line2.animate().scale(1).shift(RIGHT*4.8),Create(area3))

        circle = Circle(radius=0.5, color=PURE_RED, fill_color=YELLOW, fill_opacity=1,stroke_width=20)
        circle.move_to([-25,-3,0]) 
        self.play(Create(circle))
        #圆
       
        self.wait(0.2)
        self.play(FadeOut(area2),FadeOut(area3),graph1.animate().scale(1).shift(DOWN*3),line2.animate().scale(1).shift(LEFT*9.6))
        
        # 计算阴影区域
        area2 = axes.get_area(graph1, [-0.76, 0], bounded_graph=line_graph, color=RED, opacity=0.5)
        self.play(line2.animate().scale(1).shift(RIGHT*4.8),Create(area2))
        #箭头
        area3 = axes.get_area(graph2, [0, 0.76], bounded_graph=line_graph, color=GREEN, opacity=0.5)
        self.play(line2.animate().scale(1).shift(RIGHT*4.8),Create(area3))
        
        










        self.wait()




        
        

        
        
    

        



        

        


        




        

        

        self.wait()